const db = require('../models');
const { Op } = require('sequelize');
const moment = require('moment');
class receiptservice {
    //历史小票
    async historyReceipt(truckLic, pageNo, pageSize) {
            console.log(truckLic, pageNo, pageSize);
            let obj = new Object();
            const trucklicReceipt = await db.receipt.findAll({
                where: {
                    Trucklic: truckLic,
                    CreateTime: {
                        [Op.lte]: moment().add(-1, 'day').utc('+08:00').format(),
                        [Op.gte]: moment().add(-8, 'day').utc('+08:00').format()
                    }
                },
                'order': [
                    ["ReceiptStatus", "ASC"],
                    ["CreateTime", "DESC"]
                ],
                limit: Number(pageSize),
                offset: Number(pageNo * pageSize),
                // attributes: ["WorkMode", "CouponStatus", "ContainerPlace", "ContainerID", "CreateTime", "ReceiptStatus", "ReceiptID", "ActiveTime"]
            })
            if (trucklicReceipt.length <= 0) {
                return { "code": 0 };
            } else {
                obj["code"] = 1;
                obj["data"] = trucklicReceipt;
                return obj;
            }
        }
        //当前小票详情
    async receiptDetail(receiptId) {
            let obj = new Object();
            const resultDetail = await db.receipt.findOne({
                where: {
                    ReceiptID: receiptId
                },
                attributes: ["TruckLic", "WorkID", "ResvID", "ContainerWeight", "ContainerFrom", "ContainerType", "ContainerStatus", "ContainerOwner", "ContainerLimit", "ContainerFB"]
            })
            if (resultDetail === null) {
                return { "code": 0 }
            } else {
                obj["code"] = 1;
                obj["data"] = resultDetail;
                return obj;
            }
        }
        //当前小票
    async currentReceipt(trucklic) {
            try {
                let obj = new Object();
                let data = new Array();
                const resultR0 = await db.receipt.findAll({
                    where: {
                        Trucklic: trucklic,
                        IsReActive: 1,
                        ActiveTime: {
                            // [Op.lte]: moment().utc('+08:00').format(),
                            [Op.gte]: moment().add(-1, 'day').utc('+08:00').format()
                        }
                    },
                    'order': [
                        ["ReceiptStatus", "ASC"],
                        ["ActiveTime", "DESC"]
                    ]
                })
                data.push(resultR0);
                const resultR1 = await db.receipt.findAll({
                    where: {
                        Trucklic: trucklic,
                        IsReActive: 0,
                        CreateTime: {
                            // [Op.lte]: moment().utc('+08:00').format(),
                            [Op.gte]: moment().add(-1, 'day').utc('+08:00').format()
                        }
                    },
                    'order': [
                        ["ReceiptStatus", "ASC"],
                        ["ActiveTime", "DESC"]
                    ]
                })
                data.push(resultR1);
                if (resultR1.length <= 0 && resultR0.length <= 0) {
                    return { "code": 0 }
                } else {
                    obj["code"] = 1;
                    obj["data"] = data;
                    return obj;
                }
            } catch (error) {
                console.log(error);
            }

        }
        //去完成
    async toComplish(params) {
            const receiptId = params;
            try {
                var result = await db.receipt.update({
                    ReceiptStatus: 1
                }, {
                    where: {
                        ReceiptID: receiptId
                    }
                })
                return result;
            } catch (error) {
                console.log(error)
            }
        }
        //修改小票优惠
    async modifyReceipt(params) {
        const { ReceiptID, Direction, StatusNow } = params;
        try {
            let obj = new Object();
            if (Direction == '0') { //进方向减2
                var Status = parseInt(StatusNow) - 2;
                var result = await db.receipt.update({
                    CouponStatus: Status,
                }, {
                    where: {
                        ReceiptID: ReceiptID
                    }
                })
            } else { //出方向减1
                var Status = parseInt(StatusNow) - 1;
                var result = await db.receipt.update({
                    CouponStatus: Status,
                }, {
                    where: {
                        ReceiptID: ReceiptID
                    }
                })
            }
            console.log(result)
        } catch (error) {
            console.log(error)
        }

    }

}
module.exports = new receiptservice();